package my.pack.tm;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;

public class SolveGame extends Activity implements OnClickListener{
	static int counter = 0;
	ImageButton s1, s2, s3, s4, s5, s6, s7, s8;
	MediaPlayer mp1, mp2;
	Intent intent;
	int [] sequence, test;
	 public void onCreate (Bundle savedInstanceState){
	    	
   	  super.onCreate(savedInstanceState);
   	  setContentView(R.layout.solve);
   	  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

   	 s1 = (ImageButton) findViewById(R.id.img1);
   	 s2 = (ImageButton) findViewById(R.id.img2);
   	 s3 = (ImageButton) findViewById(R.id.img3);
   	 s4 = (ImageButton) findViewById(R.id.img4);
   	 s5 = (ImageButton) findViewById(R.id.img5);
   	 s6 = (ImageButton) findViewById(R.id.img6);
   	 s7 = (ImageButton) findViewById(R.id.img7);
   	 s8 = (ImageButton) findViewById(R.id.img8);
   	 s1.setOnClickListener(this);
   	 s2.setOnClickListener(this);
   	 s3.setOnClickListener(this);
   	 s4.setOnClickListener(this);
   	 s5.setOnClickListener(this);
   	 s6.setOnClickListener(this);
   	 s7.setOnClickListener(this);
   	 s8.setOnClickListener(this);
   	 mp1 = MediaPlayer.create(this, R.raw.soft_airy_swish);
   	 mp2 = MediaPlayer.create(this, R.raw.metal_hit);
   	 intent = getIntent();
   	 sequence = intent.getIntArrayExtra("sequence");
   	 test = new int [sequence.length];

}

	public void onClick(View v) { //each time the user selects a symbol add it to the test array and call the endGame() method
		 switch(v.getId()){
		 case R.id.img1: 
			 test[counter] = 1;
			 endGame();
			 break;
		 case R.id.img2:
			 test[counter] = 2;
			 endGame();
			 break;
		 case R.id.img3:
			 test[counter] = 3;
			 endGame();
			 break;
		 case R.id.img4:
			 test[counter] = 4;
			 endGame();
			 break;
		 case R.id.img5:
			 test[counter] = 5;
			 endGame();
			 break;
		 case R.id.img6:
			 test[counter] = 6;
			 endGame();
			 break;
		 case R.id.img7:
			 test[counter] = 7;
			 endGame();
			 break;
		 case R.id.img8:
			 test[counter] = 8;
			 endGame();
			 break;
		 
		 }

		
	}
	public void endGame(){  //compare the user's choice against the initial sequence
		 if(sequence[counter]==test[counter]){  //if correct continue
			 mp1.start();
			 counter++;
		 }
		 else {	 //else return
			 mp2.start();
			 counter=0;
			 Toast.makeText( getApplicationContext(),"Wrong!", 1000).show();
			 finish();
			}
		 
		 if(counter==sequence.length) { //when sequence is done return 
			 counter=0;
			 Toast.makeText( getApplicationContext(),"You won!", 1000).show();
			 finish();
		 }
	}
}